package guoanmaker.provider.business.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.provider.business.model.ProvideDemandDocking;
import guoanmaker.provider.business.modelnobind.DemandDocking;
import guoanmaker.provider.business.modelnobind.SpaceReserve;

/**
 * 
* @ClassName: DemandDockingDao 
* @Description: 需求对接dao
* @author Yvonne_MGL
* @date 2017年12月18日 上午10:57:00 
*  
 */
@Repository
public class DemandDockingDao {
	@PersistenceContext
	EntityManager em;
	
	/**
	 * 
	* @Description: 根据条件查询需求 
	* @param beginTime 起始时间
	* @param endTime 结束时间
	* @param likeStr 关键字：模糊查询  联系人，联系方式，企业名称，订单编号
	* @param state 1为未确认    2为已确认
	* @param pageNumber 页数
	* @param pageSize 每页条数
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午10:57:59     
	* @return Map    
	* @throws 
	 */
	public Map findByProperties(String beginTime,String endTime,String likeStr,String state,String businessid, Integer pageNumber, Integer pageSize,String userid){
		String jpql ="select new guoanmaker.provider.business.modelnobind.DemandDocking(d.id,d.enterpriseName,d.position,d.contactPerson,d.contactNumber,"
				+ "d.createTime,d.confirmTime,d.states,d.updateTime,d.sortName,d.projectIntroduction,d.orderNumber,c.commodityName,b.businessName,c.synopsis) "
				+ "from ProvideDemandDocking d  left join d.user u  left join d.commodity c left join c.providerBusiness b where 1=1";
		if(beginTime!=null&&!beginTime.equals("")){
			jpql+=" and d.createTime>='"+beginTime+"'";
		}
		if(endTime!=null&&!endTime.equals("")){
			jpql+=" and d.createTime<='"+endTime+"'";
		}
		if(likeStr!=null&&!likeStr.equals("")){
			jpql+=" and(d.contactPerson like '%"+likeStr+"%' or d.contactNumber like '%"+likeStr+"%' or d.enterpriseName like '%"+likeStr+"%'  or d.orderNumber like '%"+likeStr+"%')";
		}
		if(state!=null&&!state.equals("")){
			jpql+=" and d.states='"+state+"'";
		}
		if(businessid!=null&&!businessid.equals("")){
			jpql+=" and b.id='"+businessid+"'";
		}
		if(userid!=null&&!userid.equals("")){
			jpql+=" and u.id='"+userid+"'";
		}
		jpql+=" order by d.createTime desc";
		if(pageNumber==null){
			pageNumber=0;
		}
		if (pageSize==null) {
			pageSize=10;
		}
		Query query = em.createQuery(jpql);
		List<DemandDocking> list = query.getResultList();
		Map map=new HashMap();
		map.put("totle", list.size());
		query.setFirstResult(pageNumber*pageSize); // 序号从0 开始
		query.setMaxResults(pageSize);
		List<DemandDocking> result = query.getResultList();
		map.put("result", result);
		return map;
	}
	
	/**
	 * 
	* @Description: 根据id查询需求对接 
	* @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午11:13:00     
	* @return DemandDocking    
	* @throws 
	 */
	public DemandDocking findDemandDockingByid(String id){
		String jpql ="select new guoanmaker.provider.business.modelnobind.DemandDocking(d.id,d.enterpriseName,d.position,d.contactPerson,d.contactNumber,"
				+ "d.createTime,d.confirmTime,d.states,d.updateTime,d.sortName,d.projectIntroduction,d.orderNumber,c.commodityName,b.businessName,c.synopsis) "
				+ "from ProvideDemandDocking d  left join d.user u  left join d.commodity c left join c.providerBusiness b where d.id='"+id+"'";
		Query query = em.createQuery(jpql);
		List<DemandDocking> list = query.getResultList();
		DemandDocking demandDocking=null;
		if(list!=null&&list.size()>0){
			demandDocking=list.get(0);
		}
		return demandDocking;
	}
	
	/**
	 * 
	* @Description: 根据用户id和商品id查询需求对接详情
	* @param @param userid
	* @param @param commodityid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年1月10日 上午11:13:53     
	* @return DemandDocking    
	* @throws 
	 */
	public DemandDocking findDemandDockingByCommid(String userid,String commodityid){
		String jpql ="select new guoanmaker.provider.business.modelnobind.DemandDocking(d.id,d.enterpriseName,d.position,d.contactPerson,d.contactNumber,"
				+ "d.createTime,d.confirmTime,d.states,d.updateTime,d.sortName,d.projectIntroduction,d.orderNumber,c.commodityName,b.businessName,c.synopsis) "
				+ "from ProvideDemandDocking d  left join d.user u  left join d.commodity c left join c.providerBusiness b where u.id='"+userid+"' and c.id='"+commodityid+"'";
		Query query = em.createQuery(jpql);
		List<DemandDocking> list = query.getResultList();
		DemandDocking demandDocking=null;
		if(list!=null&&list.size()>0){
			demandDocking=list.get(0);
		}
		return demandDocking;
	}
	
	/**
	 * 
	* @Description: 查询大于今天凌晨开始生成的需求对接订单 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 下午16:10:40     
	* @return int    
	* @throws 
	 */
	public String getOrderNumber(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String timeBegin=format.format(date)+" 00:00:00";
		String jpql="select d from ProvideDemandDocking d where d.createTime>='"+timeBegin+"'";
		Query query = em.createQuery(jpql);
		List<ProvideDemandDocking> result = query.getResultList();
		int sum=0;
		if(result!=null){
			sum=result.size();
		}
		String str="N"+format.format(date)+(sum+1);
		str=str.replace("-", "");
		return str;
	}

}
